# Maintainer: Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Kyle Keen <keenerd@gmail.com>
# Contributor: Mihai Militaru <mihai militaru at xmpp dot ro>
# Contributor: carstene1ns <arch carsten-teibes.de>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - disable setting MBEDTLS_HAVE_SSE2
#  - patch to fix FTBFS with gcc 14: https://github.com/Mbed-TLS/mbedtls/issues/9003

pkgname=mbedtls
pkgver=3.6.0
pkgrel=1
pkgdesc='An open source, portable, easy to use, readable and flexible TLS library'
arch=(x86_64)
url=https://tls.mbed.org
license=(Apache-2.0)
depends=(
  glibc
  sh
)
checkdepends=(python)
makedepends=(
  cmake
  git
  ninja
  python
)
provides=(
  libmbedcrypto.so
  libmbedtls.so
  libmbedx509.so
  polarssl
)
replaces=(polarssl)
conflicts=(polarssl)
options=(staticlibs)
_tag=67dc8f869a2ef39b1f19f6a6d3a34965bc50097e
source=(
  git+https://github.com/Mbed-TLS/mbedtls.git#tag=${_tag}
  git+https://github.com/Mbed-TLS/mbedtls-framework.git
  gcc14.patch
)
b2sums=('e1baa97607fff9e83b633f2b7ea346783c61ff380a5a65accdc8100534b6b41c2dc479e465c528252d0b01ebc19c043aa3841264a87a0c8fb87a87d6b545c469'
        'SKIP'
        'b1f1ac09a40350c508d6019a4f937b73ff5322528285a71ce341eddacc09ac06392c4d91d754786250c724174947d3f2e9fca8f242275212f9adb4d9ecd65bed')

prepare() {
  cd mbedtls
  git submodule init framework
  git config submodule.framework.url "${srcdir}"/mbedtls-framework
  git -c protocol.file.allow=always submodule update framework
  #scripts/config.py set MBEDTLS_HAVE_SSE2
  scripts/config.py set MBEDTLS_THREADING_C
  scripts/config.py set MBEDTLS_THREADING_PTHREAD
  patch -p1 -i ../gcc14.patch
}

pkgver() {
  cd mbedtls
  git describe --tags | sed 's/^v//; s/^mbedtls-//'
}

build() {
  export CFLAGS+=' -ffat-lto-objects'
  cmake -S mbedtls -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_SKIP_RPATH=ON \
    -DUSE_SHARED_MBEDTLS_LIBRARY=ON \
    -DUSE_STATIC_MBEDTLS_LIBRARY=ON \
    -Wno-dev
  cmake --build build
}

check() {
  LD_LIBRARY_PATH="${srcdir}"/build/library ctest --test-dir build
}

package() {
  DESTDIR="${pkgdir}" cmake --install build

  # rename generic utils
  local _prog _baseprog
  for _prog in "${pkgdir}"/usr/bin/*; do
  _baseprog=$(basename "$_prog")
    mv -v "$_prog" "${_prog//$_baseprog/mbedtls_$_baseprog}"
  done

  # fixup static lib permissions
  chmod 644 "$pkgdir"/usr/lib/*.a
}

# vim: ts=2 sw=2 et:
